package com.dlc.longweisheng.chongdianxian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.EditText;

import com.aplus.kira.kiralibrary.tools.permission.PermissionCallback;
import com.aplus.kira.kiralibrary.tools.permission.PermissionString;
import com.aplus.kira.kiralibrary.tools.permission.PermissionUtil;
import com.dlc.longweisheng.chongdianxian.BuildConfig;
import com.dlc.longweisheng.chongdianxian.R;
import com.dlc.longweisheng.chongdianxian.pub.Constant;
import com.dlc.longweisheng.chongdianxian.pub.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.utils.PrefUtil;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        PermissionUtil.getInstance(getApplicationContext()).requestPermissons(this, new String[]{PermissionString.CAMERA, PermissionString.READ_EXTERNAL_STORAGE, PermissionString.WRITE_EXTERNAL_STORAGE},
                new PermissionCallback.OneMethodCallback() {
                    @Override
                    public void onPermissionCallback(String[] successPermissions, String[] deniedPermissions) {

                    }
                });
        etName.setText(PrefUtil.getDefault().getString("name", ""));
        etPassword.setText(PrefUtil.getDefault().getString("password", ""));

        if (BuildConfig.DEBUG) {
            etName.setText(Constant.DEFAULT_NAME);
            etPassword.setText(Constant.DEFAULT_PASSWORD);
        }
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String name = etName.getText().toString().trim();
        if (name.equals("")) {
            showOne(etName.getHint().toString());
            return;
        }
        String password = etPassword.getText().toString().trim();
        if (password.equals("")) {
            showOne(etPassword.getHint().toString());
            return;
        }
        if (name.equals(PrefUtil.getDefault().getString("name", Constant.DEFAULT_NAME)) && password.equals(PrefUtil.getDefault().getString("password", Constant.DEFAULT_PASSWORD))) {
            PrefUtil.getDefault().saveString("name", name);
            PrefUtil.getDefault().saveString("password", password);
            Intent intent = new Intent(this, MainActivity.class);
//            intent.setAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionUtil.getInstance(getApplicationContext()).onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
